﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyShop
{
    public partial class SiteMaster : MasterPage
    {

        private int _countProd;
        public int CountProd
        {
            get { return _countProd; }
            set { _countProd = value;}
        }

        public class GroupObj
        {
            public string ID { get; set; }
            public string Name { get; set; }
        }
        public List<GroupObj> DataGroup { get; set; } 
        private const string AntiXsrfTokenKey = "__AntiXsrfToken";
        private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
        private string _antiXsrfTokenValue;

        protected void Page_Init(object sender, EventArgs e)
        {
            // The code below helps to protect against XSRF attacks
            var requestCookie = Request.Cookies[AntiXsrfTokenKey];
            Guid requestCookieGuidValue;
            if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
            {
                // Use the Anti-XSRF token from the cookie
                _antiXsrfTokenValue = requestCookie.Value;
                Page.ViewStateUserKey = _antiXsrfTokenValue;
            }
            else
            {
                // Generate a new Anti-XSRF token and save to the cookie
                _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
                Page.ViewStateUserKey = _antiXsrfTokenValue;

                var responseCookie = new HttpCookie(AntiXsrfTokenKey)
                {
                    HttpOnly = true,
                    Value = _antiXsrfTokenValue
                };
                if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
                {
                    responseCookie.Secure = true;
                }
                Response.Cookies.Set(responseCookie);
            }

            Page.PreLoad += master_Page_PreLoad;
        }

        protected void master_Page_PreLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Set Anti-XSRF token
                ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
                ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
            }
            else
            {
                // Validate the Anti-XSRF token
                if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
                    || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
                {
                    throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
                }
            }
        }

        //protected void Page_Loaded(object sender, EventArgs e)
        //{
        //    List<string> listProd = new List<string>();
        //    listProd = (List<string>)Session["ProductsInBasket"];
        //    if (listProd != null)
        //    {
        //        CountProd = listProd.Count;
        //    }
        //    else
        //    {
        //        CountProd = 0;
        //    }
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            //List<string> listProd = new List<string>();
            //listProd = (List<string>)Session["ProductsInBasket"];
            //if (listProd != null)
            //{
            //    CountProd = listProd.Count;
            //}
            //else {
            //    CountProd = 0;
            //}
            
            using (SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShopConnectionString"].ConnectionString))
            {
                SqlCommand com = new SqlCommand("select * from ProductGroup", sqlcon);
                //com.Connection.Open();
                //com.ExecuteNonQuery();
                //DataTable dataTable = new DataTable("ProductTable");
                DataSet dataSet = new DataSet();
                DataTable dataTable = new DataTable();
                SqlDataAdapter dataAdapter = new SqlDataAdapter();
                dataAdapter.SelectCommand = com;
                dataAdapter.Fill(dataTable);
                dataSet.Tables.Add(dataTable);
                GetDataGroupProduct(dataTable);

            }
        }
        public void GetDataGroupProduct(DataTable prod)
        {
            List<GroupObj> mainResult = new List<GroupObj>();
            if (prod.Rows.Count != 0)
            {

                foreach (DataRow item in prod.Rows)
                {
                    mainResult.Add(new GroupObj
                    {
                        ID = item["ID"].ToString(),
                        Name = item["Name"].ToString()
                    });
                }
                DataGroup = mainResult;
                BindGroupProduct();
            }
        }
        public void OpenBasketClick(Object sender, EventArgs e)
        {
            if (Session["ProductsInBasket"] != null)
            {
                Response.Redirect("~/Basket.aspx");
            }
            
            var idx = 2;
        }
        public void BindGroupProduct()
        {
            
            //RepeaterProduct.DataSource = DataGroup;
            //RepeaterProduct.DataBind();

        }
    }
}